package com.cw.jeeyt.service.impl.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiDatabaseScriptConfigBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiDatabaseDocDao;
import com.cw.jeeyt.dao.intf.api.ApiDatabaseScriptConfigDao;
import com.cw.jeeyt.dao.model.api.ApiDatabaseDocDo;
import com.cw.jeeyt.dao.model.api.ApiDatabaseScriptConfigDo;
import com.cw.jeeyt.service.intf.api.ApiDatabaseScriptConfigService;
import com.cw.jeeyt.service.component.ApiExportDatabaseOracleScriptComponent;
import com.cw.jeeyt.service.component.ApiExportDatabaseScriptComponent;
import com.cw.jeeyt.service.converter.api.ApiDatabaseScriptConfigConverter;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.framework.utils.SpringContextUtil;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据库脚本导出配置表业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiDatabaseScriptConfigServiceImpl implements ApiDatabaseScriptConfigService {

    private static final Logger logger = LoggerFactory.getLogger(ApiDatabaseScriptConfigServiceImpl.class);
    @Autowired
    private ApiDatabaseScriptConfigDao apiDatabaseScriptConfigDao;//数据库脚本导出配置表数据库操作接口
    @Autowired
    private ApiDatabaseDocDao apiDatabaseDocDao;//数据库操作接口

    /**
     * 查询所有数据库脚本导出配置表
     */
    @Override
    public List<ApiDatabaseScriptConfigBo> queryApiDatabaseScriptConfigList() throws Exception {
        return ApiDatabaseScriptConfigConverter.doList2BoList(
            apiDatabaseScriptConfigDao.queryApiDatabaseScriptConfigList());
    }

    /**
     * 分页查询数据库脚本导出配置表
     *
     * @param apiDatabaseScriptConfigBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiDatabaseScriptConfigBo> queryByPage(ApiDatabaseScriptConfigBo apiDatabaseScriptConfigBo,
        Integer startNum, Integer pageSize, Order order) throws Exception {

        if (apiDatabaseScriptConfigBo == null) {
            logger.error("数据库脚本导出配置表为空");
            return null;
        }
        PageInfo<ApiDatabaseScriptConfigDo> pageInfo = apiDatabaseScriptConfigDao.queryByPage(
            ApiDatabaseScriptConfigConverter.bo2Do(apiDatabaseScriptConfigBo), startNum, pageSize, order);
        List<ApiDatabaseScriptConfigBo> configBos = ApiDatabaseScriptConfigConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(configBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("exportScriptKind", "std_export_script_kind");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(configBos)), dictMap);
            configBos = JSON.parseArray(jsonArray.toJSONString(), ApiDatabaseScriptConfigBo.class);
        }
        return new PageInfo<>(pageInfo, configBos);
    }

    /**
     * 根据数据库id获取数据库脚本配置信息
     *
     * @param databaseId
     * @return
     * @throws Exception
     */
    @Override
    public ApiDatabaseScriptConfigBo getByDatabaseId(Long databaseId) throws Exception {
        if (null == databaseId) {
            logger.error("--数据库id为空--");
            return null;
        }
        return ApiDatabaseScriptConfigConverter.do2Bo(apiDatabaseScriptConfigDao.getByDatabaseId(databaseId));
    }

    /**
     * 导出数据库脚本
     *
     * @param databaseId
     * @param request
     * @param response
     */
    @Override
    public void exportScript(Long databaseId, HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        ApiDatabaseDocDo apiDatabaseDocModel = apiDatabaseDocDao.getById(databaseId);
        if (apiDatabaseDocModel == null) {
            throw new Exception("数据库信息有误");
        }
        ApiDatabaseScriptConfigDo apiDatabaseScriptConfigModel = apiDatabaseScriptConfigDao.getByDatabaseId(databaseId);
        if (apiDatabaseScriptConfigModel == null) {
            throw new Exception("数据库配置信息有误");
        }
        ApiExportDatabaseScriptComponent component = SpringContextUtil.getBean(ApiExportDatabaseScriptComponent.class);
        component.exportScript(apiDatabaseDocModel, apiDatabaseScriptConfigModel, request, response);
    }

    @Override
    public void exportOracleScript(Long databaseId, HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        ApiDatabaseDocDo apiDatabaseDocModel = apiDatabaseDocDao.getById(databaseId);
        if (apiDatabaseDocModel == null) {
            throw new Exception("数据库信息有误");
        }
        ApiDatabaseScriptConfigDo apiDatabaseScriptConfigModel = apiDatabaseScriptConfigDao.getByDatabaseId(databaseId);
        if (apiDatabaseScriptConfigModel == null) {
            throw new Exception("数据库配置信息有误");
        }
        ApiExportDatabaseOracleScriptComponent component = SpringContextUtil.getBean(
            ApiExportDatabaseOracleScriptComponent.class);
        component.exportOracleScript(apiDatabaseDocModel, apiDatabaseScriptConfigModel, request, response);
    }

    /**
     * 根据主键查询数据库脚本导出配置表信息
     *
     * @return
     */
    @Override
    public ApiDatabaseScriptConfigBo getApiDatabaseScriptConfigById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return ApiDatabaseScriptConfigConverter.do2Bo(apiDatabaseScriptConfigDao.getById(id));
    }

    /**
     * 新增数据库脚本导出配置表
     *
     * @param apiDatabaseScriptConfigBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ApiDatabaseScriptConfigBo apiDatabaseScriptConfigBo) throws Exception {

        if (apiDatabaseScriptConfigBo == null) {
            logger.error("数据库脚本导出配置表为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseScriptConfigDo apiDatabaseScriptConfig = ApiDatabaseScriptConfigConverter.bo2Do(
            apiDatabaseScriptConfigBo);
        apiDatabaseScriptConfig.setCreateBy(userBo.getId());
        apiDatabaseScriptConfig.setCreateDate(new Date());
        apiDatabaseScriptConfig.setUpdateBy(userBo.getId());
        apiDatabaseScriptConfig.setUpdateDate(new Date());
        Integer result = apiDatabaseScriptConfigDao.insert(apiDatabaseScriptConfig);
        return result;
    }

    /**
     * 新增数据库脚本导出配置表不为空的字段
     *
     * @param apiDatabaseScriptConfigBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiDatabaseScriptConfigBo apiDatabaseScriptConfigBo) throws Exception {

        if (apiDatabaseScriptConfigBo == null) {
            logger.error("数据库脚本导出配置表为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseScriptConfigDo apiDatabaseScriptConfig = ApiDatabaseScriptConfigConverter.bo2Do(
            apiDatabaseScriptConfigBo);
        apiDatabaseScriptConfig.setCreateBy(userBo.getId());
        apiDatabaseScriptConfig.setCreateDate(new Date());
        apiDatabaseScriptConfig.setUpdateBy(userBo.getId());
        apiDatabaseScriptConfig.setUpdateDate(new Date());
        Integer result = apiDatabaseScriptConfigDao.insertSelective(apiDatabaseScriptConfig);
        return result;
    }

    /**
     * 更新数据库脚本导出配置表
     *
     * @param apiDatabaseScriptConfigBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiDatabaseScriptConfigBo apiDatabaseScriptConfigBo) throws Exception {

        if (apiDatabaseScriptConfigBo == null) {
            logger.error("数据库脚本导出配置表为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseScriptConfigDo apiDatabaseScriptConfig = ApiDatabaseScriptConfigConverter.bo2Do(
            apiDatabaseScriptConfigBo);
        apiDatabaseScriptConfig.setUpdateBy(userBo.getId());
        apiDatabaseScriptConfig.setUpdateDate(new Date());

        apiDatabaseScriptConfigDao.update(apiDatabaseScriptConfig);
    }

    /**
     * 更新数据库脚本导出配置表不为空的字段
     *
     * @param apiDatabaseScriptConfigBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiDatabaseScriptConfigBo apiDatabaseScriptConfigBo) throws Exception {

        if (apiDatabaseScriptConfigBo == null) {
            logger.error("数据库脚本导出配置表为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseScriptConfigDo apiDatabaseScriptConfig = ApiDatabaseScriptConfigConverter.bo2Do(
            apiDatabaseScriptConfigBo);
        apiDatabaseScriptConfig.setUpdateBy(userBo.getId());
        apiDatabaseScriptConfig.setUpdateDate(new Date());

        apiDatabaseScriptConfigDao.updateSelective(apiDatabaseScriptConfig);
    }

    /**
     * 删除数据库脚本导出配置表
     *
     * @param apiDatabaseScriptConfigBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiDatabaseScriptConfigBo apiDatabaseScriptConfigBo) throws Exception {

        if (apiDatabaseScriptConfigBo == null) {
            logger.error("数据库脚本导出配置表为空");
            return;
        }
        apiDatabaseScriptConfigDao.delete(ApiDatabaseScriptConfigConverter.bo2Do(apiDatabaseScriptConfigBo));
    }
}
